package com.shilei.visitor;

/**
 * @description:  访问者
 * @author: 81271
 * @date: 2020-04-20 19:47
 */
public class Computer {
    private ComputerPart cpu = new CPU();
    private ComputerPart memory = new Menory();
    private ComputerPart board = new Board();

    public void accept (Visitor v) {
        cpu.accept(v);
        memory.accept(v);
        board.accept(v);
    }

    public static void main(String[] args) {
        PersonVisitor personVisitor = new PersonVisitor();
        new Computer().accept(personVisitor);
        System.out.println(personVisitor.totalPrice);
    }




}

abstract class ComputerPart {
    abstract void accept(Visitor visitor);

    abstract double getPrice();
}

class CPU extends ComputerPart {

    @Override
    void accept(Visitor v) {
        v.visitCpu(this);
    }

    @Override
    double getPrice() {
        return 5000;
    }
}

class Menory extends ComputerPart {

    @Override
    void accept(Visitor v) {
        v.visitMemory(this);
    }

    @Override
    double getPrice() {
        return 7000;
    }
}

class Board extends ComputerPart {

    @Override
    void accept(Visitor v) {
        v.visitBoard(this);
    }

    @Override
    double getPrice() {
        return 6000;
    }
}

interface Visitor {
    void visitCpu(ComputerPart c);
    void visitMemory(ComputerPart c);
    void visitBoard(ComputerPart c);
}

class PersonVisitor implements Visitor {

    double totalPrice = 0;

    @Override
    public void visitCpu(ComputerPart c) {
        totalPrice += c.getPrice() * 0.75;
    }

    @Override
    public void visitMemory(ComputerPart c) {
        totalPrice += c.getPrice() * 0.75;
    }

    @Override
    public void visitBoard(ComputerPart c) {
        totalPrice += c.getPrice() * 0.85;
    }
}

class StudentVisitor implements Visitor {

    double totalPrice = 0;

    @Override
    public void visitCpu(ComputerPart c) {
        totalPrice += c.getPrice() * 0.65;
    }

    @Override
    public void visitMemory(ComputerPart c) {
        totalPrice += c.getPrice() * 0.65;
    }

    @Override
    public void visitBoard(ComputerPart c) {
        totalPrice += c.getPrice() * 0.75;
    }
}

